package com.liangsui.mss.controller.sms;

import com.liangsui.mss.service.VerificationCodeService;
import com.liangsui.mss.vo.AjaxResult;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

/**
 * SmsSendController
 *
 * @author zhangjiawei
 * @email 1533167396@qq.com
 * @date 2022/12/3 18:50
 */
@RestController
public class SmsSendController {

    @DubboReference
    private VerificationCodeService verificationCodeService;

    /**
     * 获取验证码
     * @param phoneNumber
     * @return
     */
    @PostMapping("login/sendSms/{phoneNumber}")
    public AjaxResult acquireVerifyCode(@PathVariable String phoneNumber) {
        int i = verificationCodeService.sendSms(phoneNumber);
        if (i == 0) {
            return AjaxResult.fail("请在一分钟后重试！");
        } else {
            return AjaxResult.toAjax(i);
        }
    }

    /**
     * 校验验证码
     * 注意！！！这是读取mysql数据库的数据，频繁操作数据库会造成数据库压力大的！！！
     * 仅测试使用，真正的校验验证码最好使用中间件redis设置过期时间，需要存储验证码登录数据才持久化到mysql，已改！！！
     * @param phoneNumber
     * @param code
     * @return
     */
    @GetMapping("system/sms/checkCode/{phoneNumber}/{code}")
    public AjaxResult checkCode(@PathVariable String phoneNumber, @PathVariable Integer code) {
        return AjaxResult.success(verificationCodeService.checkCode(phoneNumber, code));
    }
}
